package com.jorado.etl.service;

import com.jorado.basedata.BaseDataUtils;
import com.jorado.core.Result;
import com.jorado.core.utility.IOUtils;
import com.jorado.core.utility.StringUtils;
import com.jorado.etl.model.TagInput;
import com.jorado.etl.model.TagResult;
import com.jorado.etl.repository.ESPositionRepository;
import com.jorado.event.EventClient;
import com.jorado.json.JsonUtils;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import org.springframework.context.annotation.Description;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Description("Solr fasttext 标签服务")
public final class EsFastTextTagService extends ExportToFileImpl<Map<String, Object>> {

    private int total;
    private FastTextTagService tagService;
    private ESPositionRepository repository;

    public EsFastTextTagService(String businessKey, String distFile) {
        super(new ESPositionRepository(businessKey), distFile, false);
        this.tagService = new FastTextTagService();
        this.repository = new ESPositionRepository("position_label");
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<Map<String, Object>> dataList) {

        total += dataList.size();

        String jobTypeId;
        String jobTitle;
        String jobType;
        String jobDescription;
        String jobNum;
        String jobId;

        List<String> tagResults = new ArrayList<>();
        List<Map<String, Object>> labelResults = new ArrayList<>();
        for (Map<String, Object> doc : dataList) {

            jobId = doc.get("job_id").toString();

            jobNum = doc.get("job_num").toString();

            jobTypeId = doc.get("job_type").toString();

            jobType = BaseDataUtils.getSubJobTypeName(jobTypeId);

            jobTitle = doc.get("job_title").toString();

            jobDescription = doc.get("job_desc").toString();

//            if (StringUtils.isNullOrWhiteSpace(jobDescription) || jobType.equals("其他") || jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
//                continue;

            TagInput tagInput = new TagInput();
            tagInput.setJobId(jobId);
            tagInput.setJobNum(jobNum);
            tagInput.setJobTypeId(jobTypeId);
            tagInput.setJobType(jobType);
            tagInput.setJobTitle(jobTitle);
            tagInput.setJobDescription(jobDescription);

            TagResult tagResult = tagService.doTag(tagInput, 10);

            if (tagResult != null) {

                Map<String, Object> data = new HashMap<>();

                data.put("job_id", tagInput.getJobId());
                data.put("job_num", tagInput.getJobNum());
                data.put("job_type_id", tagInput.getJobTypeId());
                data.put("job_type", tagInput.getJobType());
                data.put("job_title", tagInput.getJobTitle());
                data.put("job_desc", tagResult.getJobDescription());
                data.put("hit", tagResult.getHit());
                data.put("label", StringUtils.joinString(tagResult.getLabel(), "|"));
                data.put("label_name", StringUtils.joinString(tagResult.getLabelName(), "|"));
                //data.put("label_value", StringUtils.joinString(tagResult.getLabelValue(), "|"));

                tagResults.add(JsonUtils.toJson(data));

                labelResults.add(data);
            }

        }

        try {
            IOUtils.appendLines(this.distFile, tagResults);
            repository.insert(labelResults);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共打标签职位:[%d]个", total));

        //TODO:对文件去重
        return Result.OK;
    }
}
